﻿///<reference path="../../scripts/jquery-1.4.4.min.js" />
///<reference path="../../scripts/jquery-ui.min.js" />
///<reference path="../../scripts/kooboo/kooboo.form.js" />

$(function () {

    //render shippings


    function renderShippings() {
        $("#shipping-container").empty();
        var isEven = false;
        for (var i = 0; i < shippings.length; i++) {
            var item = shippings[i];
            var row = $("#shipping-row").find(".template").clone();
            row.find("td:eq(0)").html(item.Name);
            row.find("td:eq(1)").html(item.Rules.length);
            row.find("input[name=Id]").val(item.Id);

            if (isEven) {
                row.addClass("even");
            }
            isEven = !isEven;

            $("#shipping-container").append(row);

        }
    };

    renderShippings();

    //events
    //1.add
    $(".shipping-add").click(function (e) {
        e.preventDefault();
        $("#shipping-form").attr("action", add_action);
        $("#shipping-form").find("input[name=Name]").val("");
        $("#shipping-form").find("input[name=Id]").val("-1");
        $("#shipping-rule-container").empty();

        $($(this).attr("href")).dialog("open");

        $("#shipping-form").find("input:first").focus();

        return false;
    });

    //2.save 
    $("#shipping-form").submit(function (e) {
        var form = $("#shipping-form");
        $(this).ajaxPost({
            success: function (item) {
                $("#shipping-dialog").dialog("close");

                var hasFound = false;
                for (var i = 0; i < shippings.length; i++) {
                    if (shippings[i].Id == item.Id) {
                        shippings[i] = item;
                        hasFound = true;
                        break;
                    }
                }

                if (hasFound == false) {
                    shippings.splice(0,0,item);
                }

                renderShippings();

            },
            box: $("#shipping-dialog")
        });

        return false;
    });


    var ruleIndex = 0;

    //2.1 Add rule
    $('.rule-add').click(function () {
        ruleIndex++;
        var row = $("#shipping-rule-row").find(".template").clone();
        row.find(".rule-index").val(ruleIndex);
        var prefix = "ShippingCostRules[" + ruleIndex + "]";
        row.find(".rule-id").attr("name", prefix + ".Id").val("-1");
        row.find(".rule-from").attr("name", prefix + ".From").val(0);
        row.find(".rule-to").attr("name", prefix + ".To").val(0);
        row.find(".rule-cost").attr("name", prefix + ".Cost").val(0);



        $("#shipping-rule-container").append(row);
    });

    //2.2 delete rule
    $("#shipping-rule-container").find(".remove").live("click", function (e) {
        e.preventDefault();
        if (confirm($(this).attr("message"))) {
            $(this).parents("tr:first").remove();
        }
        return false;
    });


    //3.edit
    $(".edit", "#shipping-container").live("click", function (e) {

        e.preventDefault();
        var id = $(this).parents("tr:first").find("input[name=Id]").val();
        var editItem;
        for (var i = 0; i < shippings.length; i++) {
            if (shippings[i].Id == id) {
                editItem = shippings[i];
                break;
            }
        }

        $("#shipping-form").attr("action", edit_action);
        $("#shipping-form").find("input[name=Name]").val(editItem.Name);
        $("#shipping-form").find("input[name=Id]").val(editItem.Id);

        $("#shipping-dialog").dialog("open");
        $("#shipping-rule-container").empty();
        for (var i = 0; i < editItem.Rules.length; i++) {
            var rule = editItem.Rules[i];
            var row = $("#shipping-rule-row").find(".template").clone();
            row.find(".rule-index").val(i);
            var prefix = "ShippingCostRules[" + i + "]";
            row.find(".rule-id").attr("name", prefix + ".Id").val(rule.Id);
            row.find(".rule-from").attr("name", prefix + ".From").val(rule.From);
            row.find(".rule-to").attr("name", prefix + ".To").val(rule.To);
            row.find(".rule-cost").attr("name", prefix + ".Cost").val(rule.Cost);

            ruleIndex = i;

            $("#shipping-rule-container").append(row);
        }

        return false;
    });


    //4.delete
    $(".delete-link", "#shipping-container").live("click", function (e) {
        e.preventDefault();
        if (confirm($(this).attr("message"))) {
            $(this).parents("form:first").submit();
        } else {
            return false;
        }
    });

    $("form", "#shipping-container").live("submit", function () {
        var row = $(this).parents("tr:first");

        $(this).ajaxPost({
            success: function () {

                row.remove();

                var id = row.find("input[name=Id]").val();
                var editItem;
                for (var i = 0; i < shippings.length; i++) {
                    if (shippings[i].Id == id) {
                        shippings.splice(i, 1);
                    }
                }
            }
        });

        return false;
    });

})